local qianxin = fk.CreateSkill {
  name = "tyj_heg__qianxin",
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"
    Fk:loadTranslationTable{
      ["tyj_heg__qianxin"]="遣信",
      [":tyj_heg__qianxin"]="与你势力相同的角色濒死时，你可以弃置任意张花色不同的牌，令与你势力相同的角色于此结算期间：需要使用【桃】时摸等量张牌；拒绝使用【桃】后弃置等量张牌。",
      ["#tyj__qianxin-dis"]="是否发动 遣信 弃置任意张牌令即将对 %dest 使用【桃】的同势力角色摸等量张牌？",
      ["#tyj_heg__qianxin_active"]="遣信",
      ["tyj_heg__qianxin-choose"]="遣信：选择 %arg 张牌弃置。",
      ["$tyj_heg__qianxin1"]="兵困绝地，将至如归！",
      ["$tyj_heg__qianxin2"]="临危之际，速速来援！",
    }
    qianxin:addEffect(fk.EnterDying,{
      can_trigger = function (self, event, target, player, data)
        if player:hasSkill(qianxin.name) then
          return H.compareKingdomWith(player,target) and #player:getCardIds('he')>0
        end
      end,
      on_cost = function (self, event, target, player, data)
        local room = player.room
        local default_discard = table.find(player:getCardIds{Player.Hand, Player.Equip}, function (id)
          return not player:prohibitDiscard(Fk:getCardById(id))
        end)
        if default_discard == nil then return false end
        local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "#tyj_heg__qianxin_active",
      prompt = "#tyj__qianxin-dis::"..target.id,
      cancelable = true,
    })
    if success and dat and dat.cards and #dat.cards>0 then
          event:setCostData(self,{cards=dat.cards})
          return true
    end
      end,
      on_use = function (self, event, target, player, data)
        local room = player.room
        local cards = event:getCostData(self).cards
        room:throwCard(cards, qianxin.name, player, player)
        data.extra_data = data.extra_data or {}
        local tos = data.extra_data.tyj_heg__qianxin_draw or {}
        table.insertIfNeed(tos,{player.id,#cards})
        data.extra_data.tyj_heg__qianxin_draw = tos
      end,
    })
    qianxin:addEffect(fk.AskForCardUse,{
can_refresh = function (self, event, target, player, data)
        if target == player then
          local room = player.room
          local dying_event = room.logic:getCurrentEvent()
          print(dying_event)
          print(dying_event.parent)
          if dying_event and dying_event.event ~= GameEvent.SkillEffect then
            local dying = dying_event.data
            if dying then
            local datax = dying.extra_data or {}
            return datax and datax.tyj_heg__qianxin_draw and #datax.tyj_heg__qianxin_draw>0
            end
          end
        end
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
          local todis =0
          local dying_event = room.logic:getCurrentEvent()--:findParent(GameEvent.Dying)
          if dying_event then
            local dying = dying_event.data
            local datax = dying.extra_data or {}
            if datax.tyj_heg__qianxin_draw and #datax.tyj_heg__qianxin_draw>0 then
              for _, tos in ipairs(datax.tyj_heg__qianxin_draw) do
                if #tos>0 then
                  local to = room:getPlayerById(tos[1])
                  if H.compareKingdomWith(player,to) then
                    room:drawCards(player,tos[2],qianxin.name)
                    todis = todis+tos[2]
                  end
                end
              end
            end
          end
          if todis >0 then
            data.extra_data = data.extra_data or {}
            data.extra_data.tyj_heg__qianxin_dis = data.extra_data.tyj_heg__qianxin_dis or 0
            data.extra_data.tyj_heg__qianxin_dis=data.extra_data.tyj_heg__qianxin_dis+todis
          end
      end,
    })
    qianxin:addEffect(fk.AfterAskForCardUse,{
        can_refresh = function (self, event, target, player, data)
        if target == player then
          return not data.result and data.extra_data and data.extra_data.tyj_heg__qianxin_dis and data.extra_data.tyj_heg__qianxin_dis>0
        end
      end,
      on_refresh = function (self, event, target, player, data)
        local room = player.room
          local num = data.extra_data.tyj_heg__qianxin_dis
          if num and num>0 then
          local ids = table.filter(player:getCardIds("he"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
          if #ids <= data.extra_data.tyj_heg__qianxin_dis then
            room:throwCard(ids, qianxin.name, player, player)
          else
            room:askToDiscard(player,{
                min_num = data.extra_data.tyj_heg__qianxin_dis,
                max_num = data.extra_data.tyj_heg__qianxin_dis,
                skill_name = qianxin.name,
                include_equip = true,
                cancelable = false,
            })
          end
        end
      end,
    })
    return qianxin